package com.example.gl_project4;

import java.util.LinkedList;

import android.renderscript.Matrix4f;

public class MatrixStack extends LinkedList<Matrix4f> {
	private static final long serialVersionUID = 1L;

	public void pushMatrix() {
		if(!this.isEmpty()) {
			Matrix4f elem = new Matrix4f();
			elem.load(this.getLast());
			this.add(elem);
		}
		else {
			Matrix4f elem = new Matrix4f();
			this.add(elem);
		}
	}
	
	public Matrix4f popMatrix() {
		if(!this.isEmpty()) {
			return this.removeLast();
		}
		else {
			return null;
		}
	}
	
	public void loadMatrix(Matrix4f matrix) {
		if(this.isEmpty()) {
			this.pushMatrix();
		}
		this.getLast().load(matrix);
	}
	
	public Matrix4f getMatrix() {
		Matrix4f result = null;
		if(!this.isEmpty()) {
			result = this.getLast();
			result.load(this.getLast());
		}
		return result;
	}
	
	public void translate(float x, float y, float z) {
		this.getLast().translate(x, y, z);
	}
	
	public void multiplyMatrix(Matrix4f matrix) {
		this.getLast().multiply(matrix);
	}
	
	public void rotate(float a, float x, float y, float z) {
		//float[] param = this.getLast().getArray();
		//Matrix.rotateM(param, 0, a, x, y, z);
		//this.getLast().load(new Matrix4f(param));
	    float base = (float)Math.sqrt(x*x+y*y+z*z);
		this.getLast().rotate(a, x/base, y/base, z/base);
	}
	
	public void scale(float x, float y, float z) {
		this.getLast().scale(x, y, z);
	}
}
